local Gravity = {}

function Gravity:new()
	local gravity = {x = 0, y = 0, smoothX = 0, smoothY = 0}
   gravity.stack = {}

	system.setAccelerometerInterval(10)

	Runtime:addEventListener( 'accelerometer', function (event)
      gravity.x = -event.yRaw
      gravity.y = -event.xRaw

      gravity.smoothX = -event.yGravity
      gravity.smoothY = -event.xGravity
   end )

   Runtime:addEventListener( 'key', function (event)
      if event.phase == 'down' then
         table.insert(gravity.stack,event.keyName)
      else
         LuaUtils:removeValueFromTable(gravity.stack,event.keyName)
      end

      gravity.x = 0
      gravity.y = 0
      if LuaUtils:tableContains(gravity.stack,'w') then
         --gravity.x = 0
         gravity.y = -1
      end
      if LuaUtils:tableContains(gravity.stack,'a') then
         gravity.x = -1
         --gravity.y = 0
      end
      if LuaUtils:tableContains(gravity.stack,'s') then
         --gravity.x = 0
         gravity.y = 1
      end
      if LuaUtils:tableContains(gravity.stack,'d') then
         gravity.x = 1
         --gravity.y = 0
      end

      if #gravity.stack == 0 then
         gravity.x = 0
         gravity.y = 0
      end

      -- if event.phase == 'down' then
      --    if event.keyName == 'w' then
      --       gravity.x = 0
      --       gravity.y = -1
      --    elseif event.keyName == 'a' then
      --       gravity.x = -1
      --       gravity.y = 0
      --    elseif event.keyName == 's' then
      --       gravity.x = 0
      --       gravity.y = 1
      --    elseif event.keyName == 'd' then
      --       gravity.x = 1
      --       gravity.y = 0
      --    else
      --       gravity.x = 0
      --       gravity.y = 0
      --    end
      -- end
      -- if event.phase == 'up' then
      --    gravity.x = 0
      --    gravity.y = 0
      -- end

      gravity.smoothX = gravity.x
      gravity.smoothY = gravity.y
   end )

	return gravity
end

return Gravity